#include "EEPROM.h"
#include "SystemInitLayer.h"
#include "Display_Layer.h"

/********************************************************************
u8EepromBuff[0]  功率限制
u8EepromBuff[1]  联动状态

u8EepromBuff[2]  A炉9档功率
u8EepromBuff[3]  A炉P档功率

u8EepromBuff[4]  B炉9档功率
u8EepromBuff[5]  B炉P档功率

u8EepromBuff[6]  C炉9档功率
u8EepromBuff[7]  C炉P档功率
********************************************************************/

//一个数据包缓存大小
static u8 xdata u8EepromBuff[EEPROM_ONE_PACK_SIZE];
static u16 xdata u16PackAddr = 0; //当前最新数据包位置

//第一次,进行默认数据保存
static void EEPROM_FirstPowerOn(void)
{
	//默认值
	stCommon_Dis_Handle.b2PowerLimit_System = 4;
	// stCommon_Dis_Handle.b1Linkage_Enable    =0;
	stCommon_Dis_Handle.b1Linkage_Enable_Old = 0;

	//保存限制的总功率设置
	u8EepromBuff[0] = stCommon_Dis_Handle.b2PowerLimit_System;

	//保存限制的联动状态
	u8EepromBuff[1] = stCommon_Dis_Handle.b1Linkage_Enable;

	//===A炉功率(210):
	u8EepromBuff[2] = ITS713D_IS_Disk_210[9];  // 9档
	u8EepromBuff[3] = ITS713D_IS_Disk_210[10]; // 10档

	//===B炉功率(160):
	u8EepromBuff[4] = ITS713D_IS_Disk_160[9];  // 9档
	u8EepromBuff[5] = ITS713D_IS_Disk_160[10]; // 10档

	//===C炉功率(320):
	u8EepromBuff[6] = ITS713D_IS_Disk_320[9];  // 9档
	u8EepromBuff[7] = ITS713D_IS_Disk_320[10]; // 10档
}
//恢复数据
static void EEPROM_DataRecovery(void)
{
	//===恢复总功率限制
	if (u8EepromBuff[0] >= 4)
		stCommon_Dis_Handle.b2PowerLimit_System = 4;
	else
		stCommon_Dis_Handle.b2PowerLimit_System = u8EepromBuff[0];

	//===恢复旧的联动状态
	// if(u8EepromBuff[1])     stCommon_Dis_Handle.b1Linkage_Enable_Old=1;
	// else                    stCommon_Dis_Handle.b1Linkage_Enable_Old=0;

	//===恢复A炉功率(210):
	if ((u8EepromBuff[2] >= Debug_Disk_210[9] - 4) && (u8EepromBuff[2] <= Debug_Disk_210[9] + 4))
		ITS713D_IS_Disk_210[9] = u8EepromBuff[2];
	if ((u8EepromBuff[3] >= Debug_Disk_210[10] - 4) && (u8EepromBuff[3] <= Debug_Disk_210[10] + 4))
		ITS713D_IS_Disk_210[10] = u8EepromBuff[3];

	//===恢复B炉功率(160):
	if ((u8EepromBuff[4] >= Debug_Disk_160[9] - 4) && (u8EepromBuff[4] <= Debug_Disk_160[9] + 4))
		ITS713D_IS_Disk_160[9] = u8EepromBuff[4];
	if ((u8EepromBuff[5] >= Debug_Disk_160[10] - 4) && (u8EepromBuff[5] <= Debug_Disk_160[10] + 4))
		ITS713D_IS_Disk_160[10] = u8EepromBuff[5];

	//===恢复C炉功率(320):
	if ((u8EepromBuff[6] >= Debug_Disk_320[9] - 4) && (u8EepromBuff[6] <= Debug_Disk_320[9] + 4))
		ITS713D_IS_Disk_320[9] = u8EepromBuff[6];
	if ((u8EepromBuff[7] >= Debug_Disk_320[10] - 4) && (u8EepromBuff[7] <= Debug_Disk_320[10] + 4))
		ITS713D_IS_Disk_320[10] = u8EepromBuff[7];
}
//写入数据
static void EEPROM_DataWrite(void)
{
	//保存限制的总功率设置
	u8EepromBuff[0] = stCommon_Dis_Handle.b2PowerLimit_System;

	//保存限制的联动状态
	u8EepromBuff[1] = stCommon_Dis_Handle.b1Linkage_Enable;

	//===A炉功率(210):
	u8EepromBuff[2] = ITS713D_IS_Disk_210[9];  // 9档
	u8EepromBuff[3] = ITS713D_IS_Disk_210[10]; // 10档

	//===B炉功率(160):
	u8EepromBuff[4] = ITS713D_IS_Disk_160[9];  // 9档
	u8EepromBuff[5] = ITS713D_IS_Disk_160[10]; // 10档

	//===C炉功率(320):
	u8EepromBuff[6] = ITS713D_IS_Disk_320[9];  // 9档
	u8EepromBuff[7] = ITS713D_IS_Disk_320[10]; // 10档
}

//上电调用,恢复数据
void EEPROM_UserInit(void)
{
	u16 i = 0;
	u16 Effective_nCnt = 0;
	u16PackAddr = 0;
	for (i = 0; i < EEPROM_ONE_PACK_SIZE; i++)
		u8EepromBuff[i] = 0x00;

	//查找最新的数据包
	for (Effective_nCnt = 0; Effective_nCnt < EEPROM_TOTAL_SIZE; Effective_nCnt += EEPROM_ONE_PACK_SIZE)
	{
		//读取一个数据包
		for (i = 0; i < EEPROM_ONE_PACK_SIZE; i++)
			u8EepromBuff[i] = Eeprom_Read_Byte(0, Effective_nCnt + i);
		//判断是不是最新的数据包
		if (u8EepromBuff[0] == 0xff)
		{
			//第一次上电
			if (Effective_nCnt == 0)
			{
				u16PackAddr = 0;	   //当前最新数据包位置
				EEPROM_FirstPowerOn(); //准备第一次上电默认保存
				//更新FLASH
				Eeprom_Erase_Page();
				TOTAL_INTERRUPT_OFF;
				Eeprom_Write_NBytes(0, 0, u8EepromBuff, EEPROM_ONE_PACK_SIZE);
				TOTAL_INTERRUPT_OFF;
			}
			//上电恢复数据
			else
			{
				//获取最新的数据包
				Effective_nCnt -= EEPROM_ONE_PACK_SIZE;
				u16PackAddr = Effective_nCnt;
				//恢复数据
				for (i = 0; i < EEPROM_ONE_PACK_SIZE; i++)
					u8EepromBuff[i] = Eeprom_Read_Byte(0, Effective_nCnt + i);
				EEPROM_DataRecovery();

				//如果写入的数据包大于余量大小,重新擦除数据,从0地址开始存储
				if (u16PackAddr > EEPROM_MEMORY_MARGIN)
				{
					//当前最新数据包位置
					u16PackAddr = 0x00;
					//更新FLASH
					Eeprom_Erase_Page();
					TOTAL_INTERRUPT_OFF;
					Eeprom_Write_NBytes(0, 0, u8EepromBuff, EEPROM_ONE_PACK_SIZE);
					TOTAL_INTERRUPT_OFF;
				}
			}
			return;
		}
	}
}
//读取最新的数据包
void EEPROM_UserRead(void)
{
	u16 i = 0;
	//恢复数据
	for (i = 0; i < EEPROM_ONE_PACK_SIZE; i++)
		u8EepromBuff[i] = Eeprom_Read_Byte(0, u16PackAddr + i);
	EEPROM_DataRecovery();
}
//写入最新的Us数据包
void EEPROM_UserWrite(void)
{
	//指向下个数据包位置
	u16PackAddr += EEPROM_ONE_PACK_SIZE;
	//写入的数据包必须小于超负荷范围
	if (u16PackAddr < EEPROM_MEMORY_OVERLOAD)
	{
		//写入数据
		EEPROM_DataWrite();
		Eeprom_Write_NBytes(0, u16PackAddr, u8EepromBuff, EEPROM_ONE_PACK_SIZE);
	}
	else
	{
		u16PackAddr = 0;
		//写入数据
		EEPROM_DataWrite();
		Eeprom_Erase_Page();
		Eeprom_Write_NBytes(0, u16PackAddr, u8EepromBuff, EEPROM_ONE_PACK_SIZE);
	}
}
